/**
 * Created by smp on 2017/5/22.
 */
var modelPath=ROOT_PATH+'/observation/observationCard/';
/**
 * 关闭layer
 */
function closeLayer() {
    layer.close(index);
}
/**
 * 使用layer 打开一个页面
 * @param title
 * @param content
 */
function openLayer(title,content) {

    index=layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        area: ['1000px', '700px'],
        content: modelPath+content
    });
}
var deptDeptId='8d81879f91e54cfcb0bf66c8b38a213f';
$.ajax({
    type: "POST",
    url: ROOT_PATH+'/permission/department/findByParentId.shtml',
    data: {parentId:0},
    dataType: "json",
    success: function(result){
        deptDeptId=result[0].id;
    }
});
var tree=null;
var getSelectedUser = function (data) {
    if(tree=="analysisTree"){
        $("#analysis").val(data[0].id);
        $("#analysisTree").textbox("setValue",data[0].realName);
    }else if(tree=="obsRep"){
        $("#obsReporter").val(data[0].id);
        $("#obsRep").textbox("setValue",data[0].realName);
    }else if(tree=="handlePeopleName"){
        $("#handlePeople").val(data[0].id);
        $("#handlePeopleName").textbox("setValue",data[0].realName);
    }else{
        $("#addUserId").val(data[0].id);
        $("#addUserIdName").textbox("setValue",data[0].realName);
    }
};
var getSelectedContractor=function (data) {
    data = data[0];
    $('#contractorName').textbox('setValue', data.name);
    $('#contractor').val(data.id);
};
function openDetailPage(id) {
    var title='观察卡详情';
    var content='detailCard.shtml?id='+id;
    openLayer(title,content);
}
$(function () {
            //初始化加载datagrid
            $('#dg1').datagrid({
                method: 'post',
                url: ROOT_PATH + '/observation/observationCard/list.shtml',
                queryParams: {
                    obsReporter:$("#obsReporter").val(),
                    deptId: deptDeptId,
                    sort: 'addTime',
                    order: 'desc'
                },
                toolbar:'#toolbar',
                //idField: 'id',
                fitColumns: true,
                pagination: true,
                rownumbers: true,
                sortOrder: true,
                striped: true,
                singleSelect:true,
                fit: true,
                columns: [[
                    {field: 'id', width: 80, checkbox: true},
                    {field: 'obsName', title: '被观察作业', width: 100,formatter:function(value,row,index){
                        return "<a href='javascript:void(0);' style='color: deepskyblue;' onclick='openDetailPage(\""+row.id+"\")'>"+value+"</a>"
                    },   sortable: true},
                    {field: 'obsDescribe', title: '情况描述', width: 80,  sortable: true},
                    {field: 'obsFoundDate', title: '观察发现日期', width: 100, sortable: true},
                    {field: 'obsDeptName', title: '被观察单位/承包商', width: 100, sortable: false},
                    {field: 'obsReporterName', title: '报告人', width: 100, sortable: false},
                    {field: 'obsType', title: '观察类型', width: 100, sortable: true ,
                        formatter: function (value, row, index) {
                            if (row.obsType==0) {
                                return '不安全行为';
                            } else if(row.obsType==1){
                                return '不安全状态';
                            }else{
                                return '推荐的安全行为';
                            }
                        }},
                    {field: 'obsStatus', title: '状态', width: 100, sortable: true},
                    {field: 'obsReporterDeptName', title: '观察单位/部门', width: 100, sortable: false},
                    {field: 'obsAddDeptName', title: '登记部门', width: 100, sortable: false},
                    {field: 'addTime', title: '登记时间', width: 100, sortable: true},
                    {field: 'obsAddName', title: '登记人', width: 100, sortable: false}
                ]]
            });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
        var obs=$("#obsReporter").val();

        $('#tb2').form('reset');
        $("#obsReporter").val(obs);

        $(".unit").css("display","block");
        $(".contractor").css("display","none");
    });
    $('#btn_search').on("click", function () {

        var deptId=deptDeptId;
        var obsName = $('#obsName').textbox("getValue");
        var obsDescribe = $('#obsDescribe').textbox("getValue");
        var obsType = $('#obsType').combobox("getValue");
        var isConstrctor = $('#isConstrctor').combobox("getValue");
        var obsDeptId = $('#obsDeptId').combotree("getValue");
        if(isConstrctor==1){
            obsDeptId = $('#contractor').val();
        }
        var obsReporter = $('#obsReporter').val();
        var analysisUser = $('#analysis').val();
        var handleUser = $('#handlePeople').val();
        var obsPlace = $('#obsPlace').textbox("getValue");
        var beginObsTime = $('#beginObsTime').datebox("getValue");
        var endObsTime = $('#endObsTime').datebox("getValue");
        var obsStatus = $('#obsStatus').textbox("getValue");
        var addDeptId = $('#addDeptId').combotree("getValue");
        var addUserId = $('#addUserId').val();
        var beginLoginTime = $('#beginLoginTime').datebox("getValue");
        var endLoginTime = $('#endLoginTime').datebox("getValue");
        $('#dg1').datagrid('load', {deptId:deptId,obsName:obsName,obsDescribe:obsDescribe,
            obsType:obsType,isConstrctor:isConstrctor,obsDeptId:obsDeptId,obsReporter:obsReporter,
            analysisUser:analysisUser,handleUser:handleUser,obsPlace:obsPlace,beginObsTime:beginObsTime,
            endObsTime:endObsTime,obsStatus:obsStatus,addDeptId:addDeptId,addUserId:addUserId,
            beginLoginTime:beginLoginTime,endLoginTime:endLoginTime, sort: 'addTime',
            order: 'desc'});
    });
    $('#btn_advanced_search').on("click", function () {
        $('#tb2').toggle();
        $('#dg1').datagrid('resize');
    });
    //初始化下拉树
    $('.parentDepartmentId').combotree({
        url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0'
    });
    $('#analysisTree').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                tree="analysisTree";
                WindowManager.openLayer({
                    type: 2,
                    title: '添加分析人',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                    callBack:getSelectedUser
                });
            }
        }],
        editable: false
    });
    $('#obsRep').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                tree="obsRep";
                WindowManager.openLayer({
                    type: 2,
                    title: '添加报告人',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                    callBack:getSelectedUser
                });
            }
        }],
        editable: false
    });
    $('#handlePeopleName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                tree="handlePeopleName";
                WindowManager.openLayer({
                    type: 2,
                    title: '添加处理人',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                    callBack:getSelectedUser
                });
            }
        }],
        editable: false
    });
    $('#addUserIdName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                tree="addUserIdName";
                WindowManager.openLayer({
                    type: 2,
                    title: '添加登记人',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                    callBack:getSelectedUser
                });
            }
        }],
        editable: false
    });
    $('#contractorName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                WindowManager.openLayer({
                    type: 2,
                    title: '选择承包商',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/contractor/contractorInfo/simpleList.shtml',
                    callBack:getSelectedContractor
                });
            }
        }],
        editable: false
    });
    $("#isConstrctor").combobox({
        onChange:function (newValue,oldValue) {
            if(newValue==1){
                $(".unit").css("display","none");
                $(".contractor").css("display","inline");
            }else{
                $(".unit").css("display","inline");
                $(".contractor").css("display","none");
            }
        }
    });
});